package com.newear.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.newear.MyApplication;
import com.newear.R;
import com.newear.bean.CustomerInfo;
import com.newear.happyDialog.CamaraCheckDialog;
import com.newear.util.HardWare;

import java.util.ArrayList;
import java.util.List;

public class HappySingleSelectActivity extends AppCompatActivity {
    private static final String TAG = "HappySingleSelectActivi";
    private LinearLayout lL_single_select_container;
    private ArrayList mSigleTextList;
    private Button btn_sigle_pre;
    private Button btn_sigle_next;
    private List<Button> yesBtns;
    private List<Button> noBtns;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_happy_single_select);
        init();
        findViews();
        setListeners();
        addSigleButtons();
    }


    private void init(){
        HardWare.registerActivity(this);
        this.mContext = this;
        mSigleTextList = new ArrayList();
        mSigleTextList.add("1、您自己是否感觉容易疲劳或乏力？");
        mSigleTextList.add("2、您平时胃口怎么样？");
        mSigleTextList.add("3、您平时容易口渴或口干吗？");
        mSigleTextList.add("4、您平时容易心烦，情绪低落吗？");
        mSigleTextList.add("5、您平时容易掉头发吗？");
        mSigleTextList.add("6、您自己感觉腰膝酸软，腰腿无力吗？");
        mSigleTextList.add("7、您平时容易失眠吗？");
        if (MyApplication.sCustomerInfo.singleSelect == null) {
            MyApplication.sCustomerInfo.singleSelect = new int[mSigleTextList.size()];
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        HardWare.unregisterActivity(this);
    }

    private void findViews(){
        lL_single_select_container = (LinearLayout) findViewById(R.id.lL_single_select_container);
        btn_sigle_pre = (Button) findViewById(R.id.btn_sigle_pre);
        btn_sigle_next = (Button) findViewById(R.id.btn_sigle_next);
    }

    private void setListeners(){
        btn_sigle_pre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btn_sigle_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean hasUnSelected = false;
                if (MyApplication.sCustomerInfo.singleSelect != null) {
                    for (int i = 0; i < MyApplication.sCustomerInfo.singleSelect.length; i++) {
                        if (MyApplication.sCustomerInfo.singleSelect[i] == 0) {
                            hasUnSelected = true;
                            break;
                        }
                    }
                    if (hasUnSelected) {
                        CamaraCheckDialog camaraCheckDialog = new CamaraCheckDialog(mContext, R.style.dialog, "请先完成所有选项", "再进入下一页", -1);
                        camaraCheckDialog.show();
                        return;
                    }
                }
                //跳转多选页面
                Intent intent = new Intent(HappySingleSelectActivity.this, HappyMultiSelectActivity.class);
                startActivity(intent);
            }
        });
    }

    //添加单选题
    private void addSigleButtons(){
        if (lL_single_select_container != null){
            lL_single_select_container.removeAllViews();
        }
        yesBtns = new ArrayList<>();
        noBtns = new ArrayList<>();
        for (int i = 0; i < mSigleTextList.size(); i++) {
            View view = View.inflate(this, R.layout.activity_happy_single_btn_layout, null);
            TextView tvAnswer = (TextView) view.findViewById(R.id.tv_answer_des);
            final Button yesBtn = (Button) view.findViewById(R.id.btn_sigle_yes);
            final Button noBtn = (Button) view.findViewById(R.id.btn_sigle_no);
            if (i == 1){
                yesBtn.setText("正常");
                noBtn.setText("不好");
            }
            if (MyApplication.sCustomerInfo.singleSelect[i] == 1) {
                yesBtn.setSelected(true);
            } else if (MyApplication.sCustomerInfo.singleSelect[i] == -1) {
                noBtn.setSelected(true);
            }
            yesBtns.add(yesBtn);
            noBtns.add(noBtn);
            tvAnswer.setText((String)mSigleTextList.get(i));
            view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            lL_single_select_container.addView(view);

            final int finalI = i;
            yesBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (yesBtn.isSelected()) {
                        yesBtn.setSelected(false);
                        MyApplication.sCustomerInfo.singleSelect[finalI] = CustomerInfo.SingleSelectType.UN_SELECTED;
                    } else {
                        yesBtn.setSelected(true);
                        if (noBtns.get(finalI).isSelected()) {
                            noBtns.get(finalI).setSelected(false);
                        }
                        MyApplication.sCustomerInfo.singleSelect[finalI] = CustomerInfo.SingleSelectType.YES;
                    }
                }
            });

            noBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (noBtn.isSelected()) {
                        noBtn.setSelected(false);
                        MyApplication.sCustomerInfo.singleSelect[finalI] = CustomerInfo.SingleSelectType.UN_SELECTED;
                    } else {
                        noBtn.setSelected(true);
                        if (yesBtns.get(finalI).isSelected()) {
                            yesBtns.get(finalI).setSelected(false);
                        }
                        MyApplication.sCustomerInfo.singleSelect[finalI] = CustomerInfo.SingleSelectType.NO;
                    }
                }
            });

        }
    }


}
